begintownscript;

variables;
short bmessage,rctx,pcs,others_ok,twondx;
string custom_m,custom_m2;

body;

beginstate 0;
break;

beginstate 1;
break;

beginstate START_STATE;
break;

beginstate 10;

	if(is_combat() == 1){
		block_entry(1);
		end();
	}

	reset_dialog();
	add_dialog_str(0,"This is the road to Rayguard Province.  Are you SURE you want to leave the Ivalice frontier?",0);
	add_dialog_choice(0,"YES.  I want to LEAVE.");
	add_dialog_choice(1,"NO.  I want to STAY.");
	bmessage = run_dialog(0);
	if(bmessage == 2){
		block_entry(1);
		end();
	}
	message_dialog("You begin the journey towards Rayguard Province and its capital of Kalzas.","");

	force_view_center(6,42);
	set_total_visibility(1);
	force_instant_terrain_redraw();
	play_sound(16);
	pause(2);

	rctx = 7;
	while(rctx <= 25){
		if(rctx == 7){
			text_bubble_on_char(9,"As you leave Ivalice,");
		}
		if(rctx == 9){
			text_bubble_on_char(9,"");
			text_bubble_on_char(9,"the air becomes cleaner.");
		}
		if(rctx == 11){
			text_bubble_on_char(9,"");
			text_bubble_on_char(9,"The grass is healthy.");
		}
		if(rctx == 13){
			text_bubble_on_char(9,"");
			if(get_flag(99,1) == 0){
				text_bubble_on_char(9,"But as you march on,");
			}
			if(get_flag(99,9) == 1){
				text_bubble_on_char(9,"And as you march on,");
			}
		}
		if(rctx == 15){
			text_bubble_on_char(9,"");
			text_bubble_on_char(9,"you can't help but feel");
		}
		if(rctx == 17){
			text_bubble_on_char(9,"");
			if(get_flag(99,0) == 1){
				text_bubble_on_char(9,"Disappointed.");
			}
			if(get_flag(99,0) == 2){
				text_bubble_on_char(9,"Relieved.");
			}
		}
		if(rctx == 19){
			text_bubble_on_char(9,"");
			if(get_flag(99,1) == 0){
				text_bubble_on_char(9,"You did nothing.");
			}
			if(get_flag(99,1) == 1){
				if(get_flag(99,0) == 1){
					text_bubble_on_char(9,"You couldn't stop him.");
				}
				if(get_flag(99,0) == 2){
					text_bubble_on_char(9,"But hells if you care now.");
				}
			}
		}
		if(rctx == 21){
			text_bubble_on_char(9,"");
			text_bubble_on_char(9,"You are greeted at Kalzas,");
		}
		if(rctx == 23){
			text_bubble_on_char(9,"");
			text_bubble_on_char(9,"And sent to Dervish Keith.");
		}

		relocate_character(9,rctx,42);
		force_view_center(rctx,42);
		force_instant_terrain_redraw();
		pause(7);

		rctx = (rctx + 1);
	}

	force_view_center(8,8);
	relocate_character(0,8,8);
	set_character_facing(0,0);
	set_total_visibility(0);
	if(char_ok(1) == TRUE){
		relocate_character(1,8,11);
		set_character_facing(1,0);
	}
	if(char_ok(2) == TRUE){
		relocate_character(2,9,11);
		set_character_facing(2,0);
	}
	if(char_ok(3) == TRUE){
		relocate_character(3,7,11);
		set_character_facing(3,0);
	}
	force_instant_terrain_redraw();
	pause(15);

	reset_dialog();
	add_dialog_str(0,"Dervish Keith looks up at you from his desk.  He seems bored and boring.  He has likely been pushing papers about ever since he completed training.  Flab tries to escape his armor from all sides.",0);
	if(get_flag(99,2) == 0){
		add_dialog_str(1,"How he got into a position of such high command is beyond you.  On the other hand, he might be something of a tactician, or perhaps he is superb at maintaining order.  At least, in ways that don't require movement.",0);
	}
	if(get_flag(99,2) == 1){
		add_dialog_str(1,"He is nothing like Malachai.  You wonder to yourself about Malachai- is he safe in Ivalice still?  Did the nephils get him, or is he still fighting valiantly as you sit here brooding about his fate?",0);
	}
	if(get_flag(99,2) == 2){
		if(get_flag(99,1) == 0){
			add_dialog_str(1,"He is nothing like Malachai.  Poor Malachai, who fought and struggled for so long against the nephils, only to have him and his dream cut down.  And you did nothing.  You ran.  You let his killers roam freely.",0);
		}
		if(get_flag(99,1) == 1){
			add_dialog_str(1,"He is nothing like Malachai.  Malachai was strong, commanding, and had conviction.  He fought until his end, and died honorably in battle.  He did all that he could and then some.  If there is an afterlife, he smiles at you from it.",0);
		}
	}
	clear_buffer();
	append_string("_Mmm...  My papers say that you ");
	if(party_size() > 1)
		append_string("all ");
	append_string("are ");
	pcs = 0;
	while(pcs <= 3){
		if(char_ok(pcs) == TRUE){
			others_ok = (pcs + 1);
			twondx = 0;
			while(others_ok < 4 && twondx < 2){
				if(char_ok(others_ok) == 1){
					twondx = (twondx + 1);
				}
				others_ok = (others_ok + 1);
			}
			if(twondx == 1){
				append_char_name(pcs);
				append_string(" and ");
			}
			if(twondx > 1){
				append_char_name(pcs);
				append_string(", ");
			}
			if(twondx < 1){
				append_char_name(pcs);
				append_string(", ");
			}
		}
		pcs = (pcs + 1);
	}
	append_string("formerly under the command of Captain Biggs.  Since your commanding officer is dead, you will have to swear an oath that you speak truthfully._  You nod and tell him so.");
	get_buffer_text(custom_m);
	add_dialog_str(2,custom_m,0);
	if(get_flag(99,1) == 0){
		if(get_flag(99,2) == 0){
			add_dialog_str(3,"You explain everything that happened.  You tell him about the ambush at the crossroads, and how the nephils had horses and magic users and were very well armed.  Your whole unit was cut down in an instant.  Dervish Keith seems distressed.",0);
		}
		if(get_flag(99,2) == 1){
			add_dialog_str(3,"You explain everything that happened.  You tell him about the ambush at the crossroads.  You tell him about Fort Ivalice which, despite still standing, is short on men and supplies.  Dervish Keith seems distressed.",0);
		}
		if(get_flag(99,2) == 2){
			add_dialog_str(3,"You explain everything that happened.  You tell him about the ambush at the crossroads.  You tell him about how you defeated nephils at their camp.  You tell him about Fort Ivalice, which had since fallen.",0);
		}
		if(party_size() > 1){
			if(get_flag(99,2) == 0){
				add_dialog_str(4,"He begins scribbling on a piece of paper.  _Mm...  Thank you, soldiers.  All of you were wise to come here.  I am ordering the attack immediately.  You did your duty.  Be proud, men._",0);
			}
			if(get_flag(99,2) == 1){
				add_dialog_str(4,"He begins scribbling on a piece of paper.  _Mm...  Thank you, soldier.  All of you were wise to come here.  I am ordering the attack immediately.  You did your duty.  Be proud, men._  Coming from him, it sounds disgusting.  He is not a real Dervish.",0);
			}
			add_dialog_str(5,"_I will see to it that you are re-assigned to a larger unit as its leaders.  The remnants of your old unit will lead the charge against the feline foe with your new one.  It is an honor to all of you._",0);
		}
		if(party_size() == 1){
			if(get_flag(99,2) == 0){
				add_dialog_str(4,"He begins scribbling on a piece of paper.  _Mm...  Thank you, soldier.  You were wise to come here.  I am ordering the attack immediately.  You did your duty.  Be proud, sir._",0);
			}
			if(get_flag(99,2) == 1){
				add_dialog_str(4,"He begins scribbling on a piece of paper.  _Mm...  Thank you, soldier.  You were wise to come here.  I am ordering the attack immediately.  You did your duty.  Be proud, sir._  Coming from him, it sounds disgusting.  He is not a real Dervish.",0);
			}
			add_dialog_str(5,"_I will see to it that you are re-assigned to a larger unit as its leader.  You will lead the charge against the feline foe.  It is your honor._",0);
		}
	}
	if(get_flag(99,1) == 1){
		add_dialog_str(4,"You tell him everything.  You tell him about the ambush, meeting Dervish Malachai, the war party, the death of Malachai when the nephils attacked Fort Ivalice...",0);
		add_dialog_str(5,"You tell him about how you broke into the abandoned Imperial fort they were using.  When you tell him about General Luthair's confession of being Eubans, he is shocked.  _That can't be!_",0);
	}
	bmessage = run_dialog(1);

	if(get_flag(99,1) == 1){
		reset_dialog();
		add_dialog_str(0,"_The way you described the warrior, with his armor and axe...  And that he was a human within the armor?  And the way you described his voice...  I cannot believe this.  Is that where he has been this entire time?_",0);
		add_dialog_str(1,"_I...  I had no idea.  This makes no sense.  But go on._",0);
		add_dialog_str(2,"You tell him about how you saw a tiger-headed nephilim named Sabertooth appear after you slew Eubans.  An atrociously large nephil named Zenbu, the same one who slew Malachai, attacked the tiger nephil.",0);
		add_dialog_str(3,"You tell him then that, according to the testimony of Zenbu, he used to be named Casimir, and was transformed one month ago.  _That's when Eubans was attacked!  And Casimir was one of his bodyguards indeed.  How strange.  But go on._",0);
		if(get_flag(99,0) == 1)
			add_dialog_str(4,"Finally, you describe how Casimir broke free from Sabertooth's trying to control his body by nobly sacrificing himself to save you.  You finish with how you used the portal the tiger-nephilim was constructing to escape back to Ivalice.",0);
		if(get_flag(99,0) == 2)
			add_dialog_str(4,"Finally, you describe how Casimir broke free from Sabertooth's trying to control his body by tossing away everything like an idiot.  You finish with how you used the portal the tiger-nephilim was constructing to escape back to Ivalice.",0);
		add_dialog_str(5,"You say nothing about your family disease.  That is not for this bloated swine to know.",0);
		bmessage = run_dialog(1);
	}

	if(get_flag(99,1) == 0){
		force_view_center(47,47);
		force_instant_terrain_redraw();
		pause(5);

		reset_dialog();
		add_dialog_str(0,"And so it goes that you are sent back into the fray with an army of soldiers behind you.  You enter Ivalice Province with superior numbers and suffer a minimal amount of casualties from their raids.",0);
		add_dialog_str(1,"You manage to learn from a captured nephil, however, that Commander Eubans has departed and that Ratbane XIV is the one leading theif forces instead.",0);
		add_dialog_str(2,"Fort Ivalice is defended fiercely, but your siege weaponry takes the fort's walls down, leaving the frail nephils helpless against your blades.",0);
		add_dialog_str(3,"The forested areas eventually fall, and troops are able to clear out the mines, making them a clear pathway for soldiers.  The abandoned fortress being used as the nephilim stronghold is flanked on all sides.",0);
		add_dialog_str(4,"The nephilim, recognizing their defeat and anxious to stay alive, hand over Ratbane XIV and negotiate for a peaceful surrender.  You personally sign the treaty with the blessings of the Dervishes present.  Ratbane is executed.",0);
		if(party_size() > 1)
			add_dialog_str(5,"You are all promoted to the rank of Dervish for your deeds in leading the army and reclaiming Ivalice.  Peace returns, and with it, cushion-lined positions as the Province Governors.  Logging and mining once again make Ivalice prosperous.",0);
		if(party_size() == 1)
			add_dialog_str(5,"You are promoted to the rank of Dervish for your deeds in leading the army and reclaiming Ivalice.  Peace returns, and with it, a cushion-lined position as the Province Governor.  Logging and mining once again make Ivalice prosperous.",0);
		bmessage = run_dialog(1);

		message_dialog("Still, even with a successful career and thousands of coins lining your pockets, something is missing...","");

		force_view_center(39,10);
		set_total_visibility(0);
		relocate_character(0,39,10);
		force_instant_terrain_redraw();
		pause(5);

		text_bubble_on_char(6,"...after which, production");
		force_instant_terrain_redraw();
		pause(10);

		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"should increase steadily.");
		force_instant_terrain_redraw();
		pause(10);

		text_bubble_on_char(6,"");
		text_bubble_on_char(0,"Very well.  I approve.");
		force_instant_terrain_redraw();
		pause(10);

		text_bubble_on_char(0,"");
		text_bubble_on_char(6,"Then with your blessings,");
		force_instant_terrain_redraw();
		pause(10);

		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"Sir, I depart.");
		force_instant_terrain_redraw();
		pause(7);

		text_bubble_on_char(6,"");
		relocate_character(6,38,7);
		set_character_facing(6,2);
		force_instant_terrain_redraw();
		pause(4);

		relocate_character(6,37,7);
		force_instant_terrain_redraw();
		pause(4);

		relocate_character(6,36,8);
		force_instant_terrain_redraw();
		pause(4);

		relocate_character(6,35,9);
		force_instant_terrain_redraw();
		pause(4);

		relocate_character(6,34,9);
		force_instant_terrain_redraw();
		pause(4);

		relocate_character(6,33,9);
		force_instant_terrain_redraw();
		pause(4);

		set_terrain(33,9,13);
		erase_char(6);
		force_instant_terrain_redraw();
		play_sound(59);
		pause(4);

		text_bubble_on_char(0,". . .");
		force_instant_terrain_redraw();
		pause(6);

		text_bubble_on_char(0,"");
		force_instant_terrain_redraw();
		pause(8);

		relocate_character(7,37,13);
		put_boom_on_space(37,13,0,0);
		force_instant_terrain_redraw();
		run_animation_sound(5);
		pause(3);

		relocate_character(0,38,11);
		force_view_center(38,11);
		set_character_facing(0,4);
		text_bubble_on_char(0,"Who are you?!");
		force_instant_terrain_redraw();
		pause(7);

		set_character_pose(7,1);
		put_jagged_zap(37,13,38,11,2);
		put_boom_on_space(38,11,3,0);
		set_character_pose(0,11);
		force_instant_terrain_redraw();
		if(get_species(0) == 0)
			run_animation_sound(29);
		if(get_species(0) == 2)
			run_animation_sound(105);
		if(get_species(0) == 3)
			run_animation_sound(103);
		pause(5);

		message_dialog("The odd nephil with the tigerish head walks towards you as you feel your will fading.  Your consciousness and body begin separating.  You can no longer keep yourself standing erect.","This tiger demon reveals a dagger.  Your eyes begin closing as he begins carving the red scar, the red star, into your back.");

		message_dialog("The end.","");

		set_character_pose(0,0);
		block_entry(1);

		end_scenario(0);
	}

	if(get_flag(99,1) == 1){
		force_view_center(47,47);
		force_instant_terrain_redraw();
		pause(5);

		reset_dialog();
		add_dialog_str(0,"The nephils in Ivalice, with their leaders slain, are anxious to surrender.  Ratbane XIV, the only remaining leader, signs a treaty with some Dervishes and peace is restored.  Dervish Keith's investigation begins.",0);
		add_dialog_str(1,"Everything that you said would be there is.  The corpses are all where you told them that they would be.  Dervish Malachai and his men are all given proper burials.  Unfortunately, nobody is able to identify what happened to the vile tiger being.",0);
		add_dialog_str(2,"Imperial Archmagi, to whom you described Sabertooth, insist that what you saw is called a _Rakshasa,_ and that they are a rare sight in this world.  They have seen ruins of a great Rakshasan culture, but now believe them to be near extinct.",0);
		add_dialog_str(3,"And of course, you'll never know exactly why Sabertooth had it in so deeply for your lineage.  But whatever he had against you, it matters not.  You beat him once, and you will be able to beat him again.",0);
		add_dialog_str(4,"Meanwhile, you received a grand surprise once the entire affair was cleared up...",0);
		bmessage = run_dialog(1);

		force_view_center(25,22);
		relocate_character(0,25,22);
		if(char_ok(1) == TRUE){
			relocate_character(1,25,21);
			set_terrain(25,21,240);
		}
		if(char_ok(2) == TRUE){
			relocate_character(2,26,21);
			set_terrain(26,21,240);
		}
		if(char_ok(3) == TRUE){
			relocate_character(3,24,21);
			set_terrain(24,21,240);
		}
		set_character_facing(1000,4);
		force_instant_terrain_redraw();
		pause(16);

		if(party_size() > 1)
			message_dialog("Your Dervish coronations!","");
		if(party_size() == 1)
			message_dialog("Your Dervish coronation!","");

		reset_dialog();
		if(party_size() > 2)
			add_dialog_str(0,"You and your partners are all promoted to Dervishes for your acts of incredible bravery and perseverance above overwhelming odds.  The Emperor himself signs your Dervish verification slips, and you are brought on parade in front of all of Kalzas.",0);
		if(party_size() == 2)
			add_dialog_str(0,"You and your partner are both promoted to Dervishes for your acts of incredible bravery and perseverance above overwhelming odds.  The Emperor himself signs your Dervish verification slips, and you are brought on parade in front of all of Kalzas.",0);
		if(party_size() == 1)
			add_dialog_str(0,"You are promoted to the rank of Dervish for your acts of incredible bravery and perseverance above overwhelming odds.  The Emperor himself signs your Dervish verification slip, and you are brought on parade in front of all of Kalzas.",0);
		if(party_size() > 2)
			add_dialog_str(1,"All of you are promoted to being joint Province Governors of Ivalice, reigning from the recently-constructed and quickly-growing city southwest of the fort.  The pay is good, but the feeling of satisfaction is much better.",0);
		if(party_size() == 2)
			add_dialog_str(1,"Both of you are promoted to being joint Province Governors of Ivalice, reigning from the recently-constructed and quickly-growing city southwest of the fort.  The pay is good, but the feeling of satisfaction is much better.",0);
		if(party_size() == 1)
			add_dialog_str(1,"You are promoted to being Province Governor of Ivalice, reigning from the recently-constructed and quickly-growing city southwest of the fort.  The pay is good, but the feeling of satisfaction is much better.",0);
		clear_buffer();
		append_string("The people deign to erect a statue in honor of your achievements, and the city renames itself to _");
		append_char_name(0);
		append_string("ville_ in your honor.  It is a very steep compliment, but you accept it nevertheless.");
		get_buffer_text(custom_m2);
		add_dialog_str(2,custom_m2,0);
		add_dialog_str(3,"And so ends your tale.  You have left your mark.  Your legacy will surpass you.",0);
		add_dialog_str(4,"- - - -",30);
		add_dialog_str(5,"The End.",0);
		bmessage = run_dialog(1);

		if(get_flag(24,0) >= 1){
			reset_dialog();
			if(get_flag(24,0) == 1)
				add_dialog_str(0,"Meanwhile, using your new clout gained from your promotion and achievements, you are able to pull a few strings behind closed doors, negotiating a deal.",0);
			if(get_flag(24,0) == 2)
				add_dialog_str(0,"Meanwhile, using your new clout gained from your promotion and achievements, you are able to pull a few strings behind closed doors, negotiating a deal and fulfilling Dervish Malachai's final unsatisfied wish.",0);
			add_dialog_str(1,"A group of elite shock troopers surround an anonymous, unassuming mansion in the obscenely rich district of Solaria.  All does not go according to plan, though.",0);
			add_dialog_str(2,"The very next day, Emperor Stewart I sends you a letter apologizing for not apprehending ex-General Foottracer.  Considering how enthusiastic Stewart I was to the invasion of Valorim, this is result not very surprising.",0);
			add_dialog_str(3,"Apparently, though, he was actually being honest this time.  The mansion, according to all testimonies, was covered in dust and not used for at least a month before the raid.",0);
			add_dialog_str(4,"Investigations confirm what you already knew- Foottracer was not killed by execution years ago.  Nevertheless, where could he be?  Why was he gone?  Or is it possible that you already...  No, that couldn't be.",0);
			if(get_flag(99,0) == 1)
				add_dialog_str(5,"And so the tale of Ivalice Province ends with little pomp and circumstance.  Your family members have their peace, the soldiers of Fort Ivalice have their peace, Dervish Malachai has peace, but most of all, you have peace.  Right, sis?",0);
			if(get_flag(99,0) == 2)
				add_dialog_str(5,"And so the tale of Ivalice Province ends with little pomp and circumstance, and all the better.  You plan on quitting the military in a few years, once you've amassed some gold.  No more drama.  This is your life, and goddamn, it's time to live it.",0);
			bmessage = run_dialog(1);
		}

		end_scenario(1);
	}

break;


